package com.wolfking.config;

import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.ApiSelector;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * 在启动类上有EnableSwagger2的注解才会加载
 * 只有在有 EnableSwagger2 全局注解才会加载bean
 * 使用spring的FactoryBean<T>注册spring的bean，没有使用@Configuration
 *
 * @author 赵伟伟(wolfking)
 * created on 2018-11-13 10:28
 */
@Component
@EnableSwagger2
public class SwaggerConfig {

    @Value("${spring.application.name}")
    private String title;
    @Value("${spring.application.name}")
    private String description;
    @Value("${info.version:1}")
    private String version;

    @Bean("zdriver")
    public Docket zdriver() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(initApiInfo())
                .groupName("zdriver")
                .select()
                .paths(ApiSelector.DEFAULT.getPathSelector())
                .apis(RequestHandlerSelectors.basePackage("com.wolfking.controller"))
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .build();
    }

    @Bean("converter")
    public Docket converter() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(initApiInfo())
                .groupName("converter")
                .select()
                .paths(ApiSelector.DEFAULT.getPathSelector())
                .apis(RequestHandlerSelectors.basePackage("com.wolfking.converter.web"))
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .build();
    }


    @SuppressWarnings("all")
    private ApiInfo initApiInfo() {
        ApiInfo apiInfo = new ApiInfo(title,
                description,
                //版本
                version,
                "",
                //作者
                "赵伟伟",
                //链接显示文字
                "",
                //网站链接
                ""
        );
        return apiInfo;
    }
}
